package com.cheyun.netsalev3.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentStatePagerAdapter
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.widget.TabBadgeView

class BadgeTabLayoutAdapter(fm: FragmentManager?, fragmentlist: ArrayList<Fragment>,context:Context) : FragmentStatePagerAdapter(fm!!) {
    val fragmentlist = fragmentlist
    var titles: ArrayList<String>? = null
    var badges: ArrayList<Int>? = null
    var mContext: Context = context

    override fun getCount(): Int = fragmentlist.size
    override fun getItem(position: Int): Fragment {
        return fragmentlist[position]
    }


    override fun getPageTitle(position: Int): CharSequence? {
        return if (titles == null) super.getPageTitle(position) else titles!![position]
    }

    fun formatBadgeNumber(value: Int): String? {
        if (value <= 0) {
            return null
        }
        return if (value < 100) {
            Integer.toString(value)
        } else "99+"
    }

    fun getTabItemView(position: Int): View {
        val view = LayoutInflater.from(mContext).inflate(R.layout.badge_tab_layout_item, null)
        val textView = view.findViewById(R.id.textview) as TextView
        textView.setText(titles?.get(position))

        val target = view.findViewById<View>(R.id.badgeview_target)
//
//        target.setText(formatBadgeNumber(badges?.get(position)!!))
        val badgeView = TabBadgeView(mContext)
        badgeView.setTargetView(target)
        badgeView.setBadgeMargin(0, 6, 10, 0)
        badgeView.setTextSize(10f)
        badgeView.setText(formatBadgeNumber(badges?.get(position)!!))

        return view
    }

}